const YoloDetector = require('../yolo');
const APP = require('../APP');
/**
 * 从首页到房间
 */
class index {
    androidBot = null;
    gameName = ['标准排位', '匹配', '狂暴']
    gameTyleReg = [
        [138, 320, 260, 353],
        [444, 319, 571, 354],
        [751, 322, 891, 353]];
    constructor(client) {
        this.GameClient = client;
        this.androidBot = this.GameClient.androidBot;
    }
    async check_pop() {
        let data = await YoloDetector.screen(this.androidBot);
        let pop_close = APP.findClass_v2(data, 'pop_close');
        if (pop_close) {
            await APP.clickRegion(this.androidBot, data[pop_close - 1].region);
            await APP.sleep(1000);
            return true;
        }
        let pop = APP.findClass_v2(data, 'pop', '提示');
        if (pop) {
            let btn_text = APP.findClass_v2(data, 'btn_text', '确定');
            if (btn_text) {
                await btn_text.click();
                await APP.sleep(1000);
                return true;
            }
        }
        return false;

    }
    async run() {
        // 1. 点击开始游戏
        let loopGameTyle = this.GameClient.userInfo.loopGameTyle;
        let loopGameItem = this.GameClient.userInfo.loopGameItem;
        let done = false;
        let clickHomeBtn = false;
        while (!done) {
            let data = await YoloDetector.screen(this.androidBot);
            if (APP.findClass_index(data, 'loding')) {
                await APP.sleep(1000);
                continue;
            }
            await this.check_pop();
            let page_select = APP.findClass_v2(data, 'page_tag', '赛季玩法');

            let page_home_startBtn = APP.findClass_v2(data, 'page_home_startBtn', '开始游戏');
            if (page_home_startBtn && !clickHomeBtn) {
                await page_home_startBtn.click();
                // 等待
                await APP.sleep();
                data = await YoloDetector.screen(this.androidBot);
                page_select = APP.findClass_v2(data, 'page_tag', '赛季玩法');
                if (!page_select) {
                    clickHomeBtn = true;
                } else {
                    await APP.sleep();
                    continue;
                }
            }
            if (page_select) {
                let selectType = APP.findClass_v2(data, 'page_selectType_active')
                if (!selectType) {
                    APP.saveScreenshot_error('模式选择');
                    debugger;
                    continue;
                }
                let selectItem = APP.findClass_v2(data, 'page_selectType_list_active')
                if (!selectItem) {
                    APP.saveScreenshot_error('模式选择');
                    debugger;
                    continue;
                }

                console.log(`当前:${selectType.text}-${selectItem.text}
目标:${loopGameTyle}-${loopGameItem}
`);
                console.log('当前模式:', selectType.text, '当前玩法:', selectItem.text)
                if (!selectType.text.includes(loopGameTyle)) {
                    await APP.findClass_v2(data, 'page_selectType_item', loopGameTyle)?.click();
                    continue;
                }
                if (!selectItem.text.includes(loopGameItem)) {
                    await APP.findClass_v2(data, 'page_selectType_list_item', loopGameItem)?.click();
                    continue;
                }
                await APP.findClass_v2(data, 'btn_text', '开始游戏')?.click();
                continue;
            }
            let page_room = await APP.findClass_v2(data, 'page_tag', loopGameTyle);
            if (page_room) {
                console.log('已到房间')
                done = true;
                await APP.findClass_v2(data, 'btn_text', '开始游戏').click();
                break;
            }
            await APP.sleep(500);
        }
        this.GameClient.addTask({ name: 'room_to_startGame' })
        return true;
    }
}

module.exports = index;